//
//  TokenDetailViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/9.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "TokenDetailViewController.h"
#import "EosService.h"
#import "TransferViewController.h"
#import "XCollectionViewController.h"
#import "MineServer.h"
#import "TransactionViewController.h"
#import "ProjectDetailsViewController.h"
@interface TokenRecordCell : XHBaseTableCell
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UILabel *subLab;
@property (nonatomic,strong) UILabel *detailLab;

@end
@implementation TokenRecordCell

- (void)setupUI{
    self.titleLab = [UILabel new];
    self.titleLab.textColor = UIColorHex(#333333);
    self.titleLab.font = kDefaultFont(15);
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.mas_equalTo(AdaptedWidth(20));
    }];
    self.subLab = [UILabel new];
    self.subLab.textColor = UIColorHex(#B5B5B5);
    self.subLab.font = kDefaultFont(12);
    [self.contentView addSubview:self.subLab];
    [self.subLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.bottom.mas_equalTo(AdaptedWidth(-5));
    }];
    self.detailLab = [UILabel new];
    self.detailLab.font = kBoldFont(16);
    [self.contentView addSubview:self.detailLab];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLab.mas_centerY);
        make.right.mas_equalTo(-kNormalSpace);
    }];
}

@end

@interface TokenDetailViewController ()
@property (nonatomic,strong) EOSService *eosService;
@property (nonatomic,weak) UILabel *balanceLab;
@property (nonatomic,strong) MineServer *mineServer;

@end

@implementation TokenDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.account_name = CURRENT_WALLET.wallet_name;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark - net
- (void)getCurrencyBalance{
    [self.eosService getAccountCurrencyBalanceCode:self.token.code account:self.account_name symbol:self.token.symbol complete:^(BOOL success, NSString * _Nonnull errorMsg, TokenInfo * _Nonnull tokenInfo) {
        if (tokenInfo) {
            self.balanceLab.text = [tokenInfo.balance getDigital];
        }
    }];
}

- (void)loadMore:(BOOL)isMore{
    self.mineServer.account = self.account_name;
    [self getCurrencyBalance];
    [self.mineServer getActionListIsMore:isMore completion:^(BOOL success, BOOL isHaveNextPage, NSString * _Nonnull msg) {
        [self endHeaderFooterRefreshingIsHaveNextPage:isHaveNextPage];
        if (success) {
            [self.tableView reloadData];
        }else{
            [self.view makeToast:msg];
        }
    }];
}

- (void)setupUI{
    self.title = XLocalized(@"Token详情");
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(120))];
    headView.backgroundColor = UIColor.whiteColor;
    UIImageView *imageV = [UIImageView new];
    [headView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(20));
        make.left.mas_equalTo(AdaptedWidth(17));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(35), AdaptedWidth(35)));
    }];
    UILabel *nameLab = [UILabel new];
    nameLab.font = kBoldFont(16);
    [headView addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imageV.mas_centerY);
        make.left.equalTo(imageV.mas_right).offset(AdaptedWidth(14));
    }];
    UIButton*btn = [UIButton new];
    [btn setTitle:XLocalized(@"项目详情") forState:UIControlStateNormal];
    [btn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
    btn.titleLabel.font = kBoldFont(13);
    [btn setBackgroundColor:UIColorHex(#FCD839)];
    [headView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imageV.mas_centerY);
        make.right.mas_equalTo(AdaptedWidth(-17));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(77), AdaptedWidth(27)));
    }];
    ViewRadius(btn, AdaptedWidth(13.5));
    
    UIView*priceBackV = [UIView new];
    priceBackV.backgroundColor = UIColorHex(#F7F7F7);
    [headView addSubview:priceBackV];
    [priceBackV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-AdaptedWidth(16));
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    UILabel*tipLab = [UILabel new];
    tipLab.textColor = UIColorHex(#333333);
    tipLab.font = kDefaultFont(15);
    [priceBackV addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(16));
        make.centerY.mas_equalTo(0);
    }];
    tipLab.text = XLocalized(@"余额");
    
    UILabel *balanceLab = [UILabel new];
    self.balanceLab = balanceLab;
    balanceLab.textColor = UIColorHex(#333333);
    balanceLab.font = kBoldFont(20);
    [priceBackV addSubview:balanceLab];
    [balanceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(AdaptedWidth(-16));
        make.centerY.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = headView;
    
    UIView *bottomV = [UIView new];
    bottomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(kTabBarHeight);
    }];
  
    
    UIButton *trunBtn = [UIButton new];
    [trunBtn setImage:IMAGE_NAMED(@"trunblack") forState:UIControlStateNormal];
    [trunBtn setTitle:[NSString stringWithFormat:@" %@",XLocalized(@"转账")] forState:UIControlStateNormal];
    [trunBtn setTitleColor:UIColorHex(#000000) forState:UIControlStateNormal];
    trunBtn.titleLabel.font = kBoldFont(17);
    [trunBtn setBackgroundColor:UIColorHex(#FCD839)];
    
    UIButton *comeBtn = [UIButton new];
    [comeBtn setImage:IMAGE_NAMED(@"come") forState:UIControlStateNormal];
    [comeBtn setTitle:[NSString stringWithFormat:@" %@",XLocalized(@"收款")] forState:UIControlStateNormal];
    [comeBtn setTitleColor:UIColorHex(#FFFFFF) forState:UIControlStateNormal];
    comeBtn.titleLabel.font = kBoldFont(17);
    [comeBtn setBackgroundColor:UIColorHex(#333333)];
    
    [bottomV addSubview:trunBtn];
    [bottomV addSubview:comeBtn];
    [trunBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.right.equalTo(comeBtn.mas_left);
        make.height.mas_equalTo(49);
    }];
    [comeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.width.height.equalTo(trunBtn);
    }];
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = AdaptedWidth(60);

    ViewRadius(imageV, AdaptedWidth(17.5));
    [imageV sd_setImageWithURL: String_To_URL(self.token.logo)];
    nameLab.text = self.token.symbol;
    
    kWeakSelf(self);
    [trunBtn addTapBlock:^(UIButton * _Nonnull btn) {
        TransferViewController *vc = [TransferViewController new];
        vc.defaultTokenInfo = weakself.token;
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    [comeBtn addTapBlock:^(UIButton * _Nonnull btn) {
        XCollectionViewController *vc = [XCollectionViewController new];
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    
    [btn addTapBlock:^(UIButton * _Nonnull btn) {
        ProjectDetailsViewController *vc = [ProjectDetailsViewController new];
        vc.tokenId = weakself.token.modelId;
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
}

#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.mineServer.transcationList.count;;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TokenRecordCell *cell = [TokenRecordCell cellForTableView:tableView];
    TranscationInfoM *info = [self.mineServer.transcationList objectAtIndex:indexPath.row].data;
    TranscationModel *model = [self.mineServer.transcationList objectAtIndex:indexPath.row];
    cell.titleLab.text = [info.from isEqualToString:self.mineServer.account]?info.to:info.from;
    cell.subLab.text = model.block_time;
    cell.detailLab.text = [NSString stringWithFormat:@"%@%@",[info.from isEqualToString:self.mineServer.account]?@"-":@"+",info.quantity];
    cell.detailLab.textColor = [info.from isEqualToString:self.mineServer.account]?UIColorHex(#FF3B3B):UIColorHex(#FFCC03);
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backV = [UIView new];
    backV.backgroundColor = UIColorHex(#F7F7F7);
    UILabel *titleLab = [[UILabel alloc]init];
    titleLab.font = kDefaultFont(12);
    titleLab.textColor = UIColorHex(#B5B5B5);
    titleLab.text = XLocalized(@"交易记录");
    [backV addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(AdaptedWidth(16));
    }];
    return backV;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return AdaptedWidth(27);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TransactionViewController *vc = [TransactionViewController new];
    vc.transcationModel = [self.mineServer.transcationList objectAtIndex:indexPath.row];
    vc.curWalletName = self.account_name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillLayoutSubviews{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.customNavigationBar.mas_bottom);
        make.bottom.mas_equalTo(-kTabBarHeight);
    }];
}

#pragma mark - getter

- (EOSService*)eosService{
    if (!_eosService) {
        _eosService = [EOSService new];
    }
    return _eosService;
}
- (MineServer*)mineServer{
    if (!_mineServer) {
        _mineServer = [MineServer new];
        _mineServer.symbol = self.token.symbol;
    }
    return _mineServer;
}
@end
